package com.baohulu.baohulu.utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Set;

/**
 * Json 工具类
 * Created by wenhaiz on 2018/3/19.
 */

public class JsonUtil {

    /**
     * 更方便的创建Json对象，简化代码
     */
    public static class JsonBuilder {
        private HashMap<String, Object> data = new HashMap<>();

        public JsonBuilder put(String name, int value) {
            data.put(name, value);
            return this;
        }

        public JsonBuilder put(String name, String value) {
            data.put(name, value);
            return this;
        }

        public JsonBuilder put(String name, double value) {
            data.put(name, value);
            return this;
        }

        public JSONObject build() {
            JSONObject object = new JSONObject();
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                try {
                    object.put(key, data.get(key));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return object;
        }
    }
}
